package com.arkisv.log.mq.receiver;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

import org.springframework.beans.factory.annotation.Autowired;

import com.arkisv.common.model.EventLog;
import com.arkisv.common.util.SystemUtil;
import com.arkisv.log.dao.EventLogMapper;
import com.arkisv.log.util.Global;

/**
 * 事件日志接收器
 *
 * @author fb
 * @date 2017年2月9日下午5:57:41
 */
public class EventLogReceiver implements MessageListener {
    @Autowired
    private EventLogMapper eventLogDao;

    @Override
    public void onMessage(Message message) {
        ObjectMessage om = (ObjectMessage) message;
        try {
            EventLog eventLog = (EventLog) om.getObject();
            SystemUtil.println("事件日志:" + eventLog);

            if(eventLog == null || eventLog.getEventId() == null) {
                return;
            }

            if(eventLog.isAdd()) {
                eventLogDao.insertSelective(eventLog);
            } else {
                // 修改前先查询是否已添加该日志(有可能修改的日志先发送过来)
                EventLog dbEventLog = eventLogDao.selectByPrimaryKey(eventLog.getEventId());
                if(dbEventLog == null) {
                    eventLog.setCacheTime(System.currentTimeMillis());
                    Global.CACHE_UPDATE_EVENTLOG_LIST.add(eventLog);
                }
                eventLogDao.updateByPrimaryKeySelective(eventLog);
            }
        } catch (JMSException e) {
            e.printStackTrace();
        }

    }
 
}
